package com.jiankang.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jiankang.entity.Order;
import com.jiankang.entity.Product;
import com.jiankang.entity.User;
import com.jiankang.service.OrderService;
import com.jiankang.service.ProductService;
import com.jiankang.service.UserService;
import com.jiankang.util.Page;

@Controller
@RequestMapping("/order")
public class OrderController {
	@Resource
	private OrderService orderService;
	@Resource
	private UserService  userService ;
	@Resource
	private ProductService productService;
	@RequestMapping("/orderproduct") 
	public String selecthealthcare(Model model, HttpServletRequest req)throws Exception {
		List<User> user=userService.getAllUser();
		String pageNow = req.getParameter("pageNow");
		Page page = null;
		List<Order> order=null;
		int totalCount = (int) orderService.getAllProductCount();
		if (pageNow != null) {
			page = new Page(totalCount, Integer.parseInt(pageNow));
			order=orderService.getHealthFile(page);
		}else{
			page = new Page(totalCount, 1);
			order=orderService.getHealthFile(page);
		}
		List<Product> product=productService.product();
		for(Order o:order){
			String str = o.getProductId().trim();
			String[] a = str.split(",");
			String tempProductName="";
			for(int i=0;i<a.length;i++){
				for(Product p:product){ 
					if(Integer.parseInt(a[i].trim())==p.getId()){
						tempProductName+=p.getTitle()+",";
						o.setProductId(tempProductName.substring(0, tempProductName.length()-1));
					}
				}
			}
		}
		model.addAttribute("page", page);
		model.addAttribute("order", order);
		model.addAttribute("user", user);
		return "admin/order";

	}
}
